package com.newer.service.sys.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.newer.dao.sys.mapper.OrganizationMapper;
import com.newer.pojo.sys.Organization;
import com.newer.pojo.sys.OrganizationExample;
import com.newer.pojo.sys.OrganizationExample.Criteria;
import com.newer.service.sys.iter.OrganizationService;
import com.newer.utils.sys.BootstrapDataTable;

@Service
public class OrganizationServiceImpl implements OrganizationService {

	@Autowired
	private OrganizationMapper organizationMapper;

	/**
	 * 显示部门列表
	 */
	@Override
	public BootstrapDataTable tableView(
			BootstrapDataTable dataTable) {
		List<Organization>organizationList=organizationMapper.selectByExample(null);
		BootstrapDataTable bd = new BootstrapDataTable();
		// 一页的数据
		bd.setRows(organizationList);
		// 总记录数
		bd.setTotal(organizationList.size());
		return bd;
	}


	//检查部门是否存在
	@Override
	public Map<String, Boolean> selectOrganizationByOrganizationName(
			String organizationName) {
		// TODO Auto-generated method stub
		System.out.println("部门名"+organizationName);
		boolean result = true;
		OrganizationExample example = new OrganizationExample();
		Criteria  criteria=example.createCriteria();
		criteria.andOrganizationNameEqualTo(organizationName);
		List<Organization>list=organizationMapper.selectByExample(example);
		if(list.size()>0){
			result=false;
		}
		Map<String , Boolean>map=new HashMap<String,Boolean>();
		map.put("valid", result);
		return map;
	}

	/**
	 * 修改部门
	 */
	@Override
	public int updateOrganizationByPrimaryKey(Organization organization) {
		// TODO Auto-generated method stub
		System.out.println("修改部门"+organization);
		return organizationMapper.updateByPrimaryKeySelective(organization);
	}

	/**
	 * 根据id查询部门
	 */
	@Override
	public Organization selectOrganizationById(Integer id) {
		// TODO Auto-generated method stub
		Organization selectByPrimarykey=organizationMapper.selectByPrimaryKey(id);
		return selectByPrimarykey;
	}

	//删除
	@Override
	public boolean deleteOrganization(List<Integer> ids) {
		// TODO Auto-generated method stub
		try {
			// 执行删除
			organizationMapper.deleteOrganization(ids);
			// 查询是否有子集
			List<Integer> list = organizationMapper.selectNodeIds(ids);
			if (list != null && !list.isEmpty()) {
				deleteOrganization(list);
			}
		} catch (Exception e) {
			e.printStackTrace();
			// 删除失败
			return false;
		}
		// true
		return true;
	}

	//添加部门
	@Override
	public int addOrganization(Organization organization) {
		// TODO Auto-generated method stub
		organization.setDeleteStatus("0");
		return organizationMapper.insert(organization) ;
	}
}
